package com.bio.dome;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BIOServer {

    //服务端网络IO模型的封装对象
    ServerSocket serverSocket;

    //服务器
    public BIOServer(int port){
        try {
            serverSocket = new ServerSocket(port);
            System.out.println("服务端启动，端口号："+port);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public void listen() throws  IOException{
        //循环监听
        while (true){
             //等待客户端连接，阻塞方法
            //socket数据发送者在服务端的引用
            Socket client = serverSocket.accept();

            //对方发数据给我，读流
            InputStream is = client.getInputStream();
            byte[] buff = new byte[1024];
            int len = is.read(buff);
            if(len>0){
                String msg = new String(buff, 0, len);
                System.out.println("收到"+msg);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new BIOServer(8080).listen();
    }
}
